package com.campus.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.campus.dao.DiscussDao;
import com.campus.entity.Discuss;

@Repository
public class DiscussDaoImpl implements DiscussDao {

	@Resource
	private SessionFactory sessionFactory;
	
	@Override
	public List<Discuss> selectList() {
		List<Discuss> list = (List<Discuss>) sessionFactory.getCurrentSession().createQuery("from Discuss").list();
		return list;
	}

	@Override
	public void add(Discuss thisClass) {
		sessionFactory.getCurrentSession().save(thisClass);
	}

	@Override
	public Discuss getById(int id) {
		Discuss myClass = (Discuss) sessionFactory.getCurrentSession().get(Discuss.class, id);
		return myClass;
	}

	@Override
	public void update(Discuss thisClass) {
		sessionFactory.getCurrentSession().update(thisClass);
	}

	@Override
	public void deleteById(int id) {
		Discuss c=new Discuss();
		c.setId(id);
		sessionFactory.getCurrentSession().delete(c);
	}

	@Override
	public List<Discuss> pagingList(int curPage, int pageCount) {
		List<Discuss> list = (List<Discuss>) sessionFactory.getCurrentSession().createQuery("from Discuss").setMaxResults(pageCount)
				.setFirstResult((curPage - 1) * pageCount).list();
		return list;
	}

	@Override
	public int countAll() {
		long n = (Long) sessionFactory.getCurrentSession().createQuery("Select count(u) from Discuss u").uniqueResult();
		return (int)n;
	}



	@Override
	public List<Discuss> checkName(String content) {
		List<Discuss> list = (List<Discuss>) sessionFactory.getCurrentSession().createQuery("from Discuss where content= ?")
				.setParameter(0,content)
				.list();
		return list;
	}

	@Override
	public List<Discuss> checkLikeName(String content) {
		List<Discuss> list = (List<Discuss>) sessionFactory.getCurrentSession().createQuery("from Discuss where content like ?")
				.setParameter(0,"%"+content+"%")
				.list();
		return list;
	}



}
